package openase.audiosearch.ranking;

import openase.audio.AudioInfo;
import openase.audiosearch.FingerprintMatch;

import org.apache.commons.collections.Bag;
import org.apache.commons.collections.bag.HashBag;


public class MatchesCountRankingStrategy implements RankingStrategy {

    @Override
    public AudioRanking rank(FingerprintMatch[] matchingFps) {
        Bag bag = new HashBag();
        for (FingerprintMatch match: matchingFps) {
            // just add this source to the bag
            AudioInfo source = match.getRepositoryFingerprint().getSource();
            bag.add(source);
        }
        
        final AudioRankingBuilder builder = new AudioRankingBuilder();
        
        for (Object obj : bag.uniqueSet()) {
            AudioInfo audio = (AudioInfo) obj;
            // retrieve the amount of similarities for this audio
            int count = bag.getCount(audio);
            
            builder.add(audio, count);
        }
        
        return builder.getAudioRanking();
    }

}
